/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.admin.client.components;

import java.util.ArrayList;
import java.util.List;

import com.elf.gwt.admin.client.AdminComponentController;
import com.elf.gwt.common.client.components.CommonSimpleComponent;
import com.elf.gwt.common.client.model.GroupModel;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTMLTable;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.Widget;

/**
 *  
 */
public class GroupComponent extends JSONServerComponent
{
	protected Grid bodyPanel;
	protected List<GroupModel> groupModels;
	protected GroupModel selectedGroupModel = null;
	protected boolean refreshGUI = true;
	protected int index;

	/**
	 * Constructor
	 */
	public GroupComponent(AdminComponentController controller, String stylePrefix)
	{
		super(controller, stylePrefix);
		groupModels = new ArrayList<GroupModel>();
	}

	protected void buildMetaModel(JSONObject jsonObject) throws Exception
	{
	}

	/**
	 * BuildData from JSONArray
	 * @param jsonArray
	 */
	protected void buildModel(JSONArray jsonArray) throws Exception
	{
		groupModels.clear();
		refreshGUI = true;
		super.buildModel(jsonArray);
	}

	protected void buildModel(JSONObject jsonObject) throws Exception
	{
		groupModels.add(new GroupModel(jsonObject));
	}

	public void buildFromModel() throws Exception
	{
		if (selectedGroupModel == null) 
		{
			selectedGroupModel = groupModels.get(0);
		}

		if (refreshGUI)
		{
			int sz = groupModels.size();
			bodyPanel = new Grid(sz, 1);
			bodyPanel.setWidth("100%");
			bodyPanel.addClickHandler(menuHandler);
			for (int i = 0; i < sz; i++)
			{
				GroupModel groupModel = groupModels.get(i);
				InlineLabel label = new InlineLabel(groupModel.getTitle());
				label.setStyleName("ComponentLink");
				bodyPanel.setWidget(i, 0, label);
			}

			clear();
			add(bodyPanel);
			refreshGUI = false;
		}
		setSelectedInGUI();
		onBuilt();
	}

	public void setSelectedInGUI()
	{
		int sz = groupModels.size();
		for (int i = 0; i < sz; i++)
		{
			Widget widget = bodyPanel.getWidget(i, 0);
			bodyPanel.getRowFormatter().removeStyleName(i, "Selected");
			widget.removeStyleName("Selected");
			GroupModel groupModel = groupModels.get(i);
			if (selectedGroupModel.getId() == groupModel.getId()) 
				bodyPanel.getRowFormatter().addStyleName(i, "Selected");
		}
	}

	protected ClickHandler menuHandler= new ClickHandler() {
		public void onClick(ClickEvent event)
		{
			if (!JSONServerComponent.inuse) {
				HTMLTable.Cell cell = bodyPanel.getCellForEvent(event);
				int index = cell.getRowIndex();
				selectedGroupModel = groupModels.get(index);
				int loc = getHandle().indexOf("Group");
				String memberComponentHandle = getHandle().substring(0, loc) + "Component";
				String history = memberComponentHandle + "--" + selectedGroupModel.getId();
				setSelectedInGUI();
				onSelection(history);
			}
		}
	};

	public GroupModel getSelectedGroupModel() {
		return selectedGroupModel;
	}

	public void setSelectedGroupModel(int id)
	{
		int sz = groupModels.size();
		for (int i = 0; i < sz; i++)
		{
			GroupModel groupModel = groupModels.get(i);
			if (id == groupModel.getId()) selectedGroupModel = groupModel;
		}
	}

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}
	
	public CommonSimpleComponent getMemberComponent(String handle) {
		return null;
	}
}
